/*
 * @Author: viivLgr 
 * @Date: 2018-03-20 11:16:22 
 * @Last Modified by: viivLgr
 * @Last Modified time: 2018-03-21 16:51:52
 */
'use strict';
require('./index.css');
require('page/common/nav-simple/index.js');
var _util = require('util/util.js');
var _user = require('service/user-service.js');

// 表单里的错误提示
var formError = {
    show: function(errMsg){
        $('.error-item').show().find('.err-msg').text(errMsg);
    },
    hide: function(){
        $('.error-item').hide().find('.err-msg').text('');
    }
};

// page逻辑部分
var page = {
    init: function(){
        this.bindEvent();
    },
    bindEvent: function(){
        var _this = this;
        // 验证username
        $('#username').blur(function(){
            var username = $.trim($(this).val());
            _this.checkUsername(username);
        });
        // 按钮的点击
        $('#submit').click(function(){
            _this.submit();
        });
        // 按下回车提交
        $('.user-content').keyup(function(e){
            if(e.keyCode === 13){
                _this.submit();
            }
        });
    },
    // 检测用户名是否可用
    checkUsername: function(username){
        // 异步验证用户名是否存在
        if(!username) return;
        _user.checkUsername(username, function(err){
            formError.hide();
        }, function(err){
            formError.show(err);
        });
    },
    // 提交表单
    submit: function(){
        var formDate = {
                username: $.trim($('#username').val()),
                password: $.trim($('#password').val()),
                passwordConfirm: $.trim($('#password-confirm').val()),
                phone: $.trim($('#phone').val()),
                email: $.trim($('#email').val()),
                question: $.trim($('#question').val()),
                answer: $.trim($('#answer').val())
            },
            // 表单验证结果
            validateResult = this.formValidate(formDate);
        
        if(validateResult.status){
            _user.register(formDate, function(res){
                window.location.href = './result.html?type=register';
            }, function(err){
                formError.show(err);
            });
        }else{
            // 错误提示
            formError.show(validateResult.msg);
        }
    },
    formValidate: function(formData){
        var result = {
            status: false,
            msg: ''
        };
        console.log('formdata', formData);
        // 验证用户名
        if(!_util.validate(formData.username, 'require')){
            result.msg = '用户名不能为空';
            return result;
        }
        if(!_util.validate(formData.password, 'require')){
            result.msg = '密码不能为空';
            return result;
        }
        // 验证密码的长度
        if(formData.password.length < 6){
            result.msg = '密码长度不能少于6位';
            return result;
        }
        if(formData.password !== formData.passwordConfirm){
            result.msg = '两次输入的密码不一致';
            return result;
        }
        // 验证手机号
        if(!_util.validate(formData.phone, 'phone')){
            result.msg = '手机号格式不正确';
            return result;
        }
        // 验证邮箱
        if(!_util.validate(formData.email, 'email')){
            result.msg = '邮箱格式不正确';
            return result;
        }
        // 密码提示问题是否为空
        if(!_util.validate(formData.question, 'require')){
            result.msg = '密码提示问题不能为空';
            return result;
        }
        // 密码提示答案是否为空
        if(!_util.validate(formData.answer, 'require')){
            result.msg = '密码提示答案不能为空';
            return result;
        }
        // 通过验证，返回正确提示
        result.status = true;
        result.msg = '验证通过';
        return result;
    }
};
$(function(){
    page.init();
});